package com.example.shopping_cart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.shopping_cart.mapper.ShoppingCartMapper;
import com.example.shopping_cart.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shopping_cart_model.entity.ShoppingCart;
import org.jetbrains.annotations.Nls;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

import static java.time.LocalTime.now;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author sjz
 * @since 2024-07-10
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Override
    public ShoppingCart addDish(ShoppingCart shoppingCart) {
        //判断当前购物车是否已经存在该菜品/套餐
        String dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        //判断添加的是菜品还是套餐
        if (dishId != null) {
            //如果添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cartOne = this.getOne(queryWrapper);
        //判断当前购物车是否已经存在该菜品/套餐
        if (cartOne != null) {
            //存在,数量在原基础上加1
            Integer number = cartOne.getNumber();
            cartOne.setNumber(number + 1);
            this.updateById(cartOne);
        } else {
            if (shoppingCart.getNumber()==null){
                //不存在,默认数量设置为1,添加到购物车
                shoppingCart.setNumber(1);
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            this.save(shoppingCart);
            cartOne = shoppingCart;
        }

        return cartOne;
    }

    @Override
    public ShoppingCart subDish(ShoppingCart shoppingCart) {
        //判断当前点击的是菜品还是套餐
        String dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        if (dishId != null) {
            //点击的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //点击的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //获取点击的菜品/套餐
        ShoppingCart cartOne = this.getOne(queryWrapper);
        //获取该菜品/套餐的数量
        if (cartOne!= null){
            Integer number = cartOne.getNumber();
            if (number > 1) {
                //如果number不为1,number--
                cartOne.setNumber(number - 1);
                this.updateById(cartOne);
            } else {
                // 如果number为1,删除该记录
                this.removeById(cartOne.getId());
                cartOne = shoppingCart;
            }
        }


        return cartOne;
    }
}
